package com.rc.saas.mini.component;

import com.github.pagehelper.util.StringUtil;
import com.rc.saas.mini.common.config.UpYunConfig;
import com.rc.saas.mini.common.upyun.WebUploader;
import com.rc.saas.mini.common.utils.FileHandler;
import com.rc.saas.mini.utils.AliyunOssUtil;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;

/**
 * @author frog
 * @date 2021/5/25
 */
@Component
public class UploadComponent {

    private static final String TenantFolderTemp = "/{0}";

    @Autowired
    private UpYunConfig upYunConfig;


    /**
     * 上传图片
     *
     * @param uploadFile
     * @return
     */
    public String uploadFile(String tenantCode,Long memberId, MultipartFile uploadFile) {

        if (uploadFile == null) {
            return null;
        }
        //WebUploader webUploader = new WebUploader(upYunConfig);
        WebUploader webUploader = WebUploader.getInstance(upYunConfig);
        String uploadFileName = null;
        try {
            uploadFileName = uploadFile(tenantCode,memberId, webUploader, uploadFile, null);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if (StringUtils.isBlank(uploadFileName)) {
            return null;
        }

        return uploadFileName;
    }


    /**
     * 上传到CDN，并返回存储的路径
     *
     * @param webUploader
     * @param file
     * @return
     * @throws NoSuchAlgorithmException
     * @throws SignatureException
     * @throws InvalidKeyException
     */
    public String uploadFile(String tenantCode,Long memberId,WebUploader webUploader, MultipartFile file, String fileType) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, IOException {
        //CDN存放路径:/运营商编号/文件
        //String uploadFileName = upYunConfig.getUploadImagePath();

        String uploadFileName = tenantCode + "/" + memberId + upYunConfig.getUploadImagePath();
        if (StringUtil.isNotEmpty(fileType)) {
            uploadFileName = tenantCode + fileType;
        }
        uploadFileName = uploadFileName + FileHandler.createFileNameByTimeRole(file.getOriginalFilename());

        //本地硬盘存放路径
        //CommonsMultipartFile cf = (CommonsMultipartFile) file;
        //DiskFileItem df = (DiskFileItem) cf.getFileItem();
        //File localFile = df.getStoreLocation();
        //File dfile = null;
        //dfile = File.createTempFile("prefix", "_" + file.getOriginalFilename());
        //file.transferTo(dfile);
        //boolean result = webUploader.uploadFile(uploadFileName, dfile);
        //dfile.delete();
        //if (result) {
        //    return uploadFileName;
        String url=AliyunOssUtil.FilePut(file,uploadFileName);
        if (url!=null) {
            return url;
        } else {
            return null;
        }
    }


    public boolean deleteFile(String path) {
        try {
            //WebUploader webUploader = WebUploader.getInstance(upYunConfig);
            //return webUploader.deleteFile(path);
            AliyunOssUtil.deletePut(path);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public String rootPath(){
        return upYunConfig.getDownloadRoot();
    }
}
